package com.atguigu.xuexiplus.controller;

import com.atguigu.xuexiplus.entity.Subject;
import com.atguigu.xuexiplus.mapper.SubjectMapper;
import com.atguigu.xuexiplus.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/api/test")
@CrossOrigin(origins = "*")
public class TestController {

    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 测试数据库连接
     */
    @GetMapping("/database")
    public Result<String> testDatabase() {
        try {
            List<Subject> subjects = subjectMapper.selectList(null);
            return Result.success("数据库连接正常，学科数量: " + subjects.size());
        } catch (Exception e) {
            return Result.error("数据库连接失败: " + e.getMessage());
        }
    }

    /**
     * 测试学科查询
     */
    @GetMapping("/subjects")
    public Result<List<Subject>> testSubjects() {
        try {
            List<Subject> subjects = subjectMapper.selectList(null);
            return Result.success(subjects);
        } catch (Exception e) {
            return Result.error("查询学科失败: " + e.getMessage());
        }
    }
}



